package cn.itcast.controller;

import cn.itcast.pojo.Clazz;
import cn.itcast.pojo.PageInfo;
import cn.itcast.pojo.QuaryParma;
import cn.itcast.pojo.Result;
import cn.itcast.service.ClazzService;


import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    //添加班级
    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz){
        boolean result = clazzService.addClazz(clazz);
        return result ? Result.success("添加成功") : Result.erro("添加失败");
    }
    //根据id查询
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        Clazz result = clazzService.findById(id);

       return result == null? Result.erro("查询失败") : Result.success(result);
    }

    //分页
    @GetMapping
    public Result findByPage(QuaryParma quaryParma){
        PageInfo byPage = clazzService.findByPage(quaryParma);

        return byPage != null ? Result.success(byPage) : Result.erro("未查询到数据");
    }
    //更新班级信息
    @PutMapping
    public Result upDate(@RequestBody Clazz clazz){
        System.out.println("shu[====================================="+clazz);
//        log.info("claz:{}",clazz);
        boolean b = clazzService.upDate(clazz);

        return b ? Result.success("修改成功") : Result.erro("修改失败");
    }
    //删除班级
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") int id){
        boolean result = clazzService.deleteById(id);

        return result ? Result.success("删除成功") : Result.erro("删除失败");
    }

    @GetMapping("/list")
    public Result findAll(){
        List<Clazz> clazz = clazzService.findAll();

        return clazz != null ? Result.success(clazz) : Result.erro("查询失败");
    }

}
